BTBehaviorManager = {}

local this = BTBehaviorManager
this.printTreeStr = ""
this.trees = {}

function this.RunTree()
	while (true) do
		local isAllFinish = this.OnUpdate()
		if (isAllFinish) then
			break
		end
	end
end

function this.OnUpdate()
	local count = 0
	for i = 1, #this.trees do
		local tree = this.trees[i]
		if (tree.executionStatus == BTTaskStatus.Inactive) then --第一次执行
			-- print("第一次执行:" .. tree.name)
			tree:OnUpdate()
		elseif (tree.executionStatus == BTTaskStatus.Running) then --第二次以及以后执行
			-- print("第二次以及以后执行:" .. tree.name)
			tree:OnUpdate()
		else
			count = count + 1
		end
	end
	-- print("------------------------------")
	if (count == #this.trees) then
		return true
	else
		return false
	end
end

--添加行为树
function this.AddTree(task)
	table.insert(this.trees, task)
end

--深度优先，打印树结构
function this.PrintTree(task)
	this.printTreeStr = ""
	this.AddToPrintTreeStr(task)
	print(this.printTreeStr)
end

function this.AddToPrintTreeStr(task)
	local taskType = task.taskType
	this.printTreeStr = this.printTreeStr .. task:ToString() .. "\n"
	if (taskType == BTTaskType.Root) then	
		this.AddToPrintTreeStr(task.startTask)
	elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
		for i = 1  ,#task.childTasks do
			this.AddToPrintTreeStr(task.childTasks[i])
		end
	else
	end
end